﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BizCollegeWebApp.Models;

namespace BizCollegeWebApp.Controllers
{
    public class AccountController : Controller
    {
        private BizCollegeDBDataContext db = new BizCollegeDBDataContext();

        // GET: /Account/

        [HttpGet]
        public ActionResult LogOn()
        {
            return View("LogOn");
        }

        [HttpGet]
        public ActionResult Welcome()
        {
            ViewBag.Message = "Welcome registered user";
            return View();
        }

        [HttpGet]
        public ActionResult LogOut()
        {
            Session.Clear();
            ViewBag.Message = "Thank you...";
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(string Username, string Password)
        {
            if (ModelState.IsValid)
            {
                BizCollegeWebApp.Models.User dbUser = this.db.Users.FirstOrDefault(u => u.Username == Username);

                if (dbUser != null && dbUser.Password == Password)
                {
                    // Success
                    Session["user"] = dbUser;
                    ViewData["msg"] = "Welcome " + dbUser.Username;
                    return RedirectToAction("Welcome");
                }
                {
                    ModelState.AddModelError("", "Invalid username or password");
                }

            }
            return View();
        }


        [HttpGet]
        public ActionResult Profile()
        {
            ViewBag.Enrollments = this.db.Enrollments.Where(e => e.UserID == Current.User.ID).AsEnumerable();

            return View(Current.User);
        }


    }

}
